<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
	<h4 class="modal-title"><font color="red">部门管理【<label id="orgLab"></label>】</font>&nbsp;</h4>
</div>
<div class="modal-body">
	<form class="form-horizontal" role="form" id="deptForm">
		<input type="hidden" name="doCode"  value="deptSave"/>
		<input type="hidden" name="method"  value="save"/>
					          <!-- Custom Tabs -->
		<div class="nav-tabs-custom">
			<div role="toolbar" class="btn-toolbar">
			<div class="btn-group">
				<button aria-label="Left Align" class="btn btn-default" type="button" onclick="addRowDept();"><span aria-hidden="true" class="glyphicon glyphicon-plus"></span>增加一级部门</button>
				<button aria-label="Center Align" class="btn btn-default" type="button" onclick="delDept(0);"><span aria-hidden="true" class="glyphicon glyphicon-minus	"></span>删除</button>
				<button aria-label="Right Align" class="btn btn-default" type="button" onclick="saveDept(0);"><span aria-hidden="true" class="glyphicon glyphicon-pencil"></span>修改</button>
				<button aria-label="Justify" class="btn btn-default" type="button" onclick="deptUserList(0);"><span aria-hidden="true" class="glyphicon glyphicon-search"></span>部门人员</button>
				<button aria-label="Justify" class="btn btn-default" type="button" onclick="deptActsList(0);"><span aria-hidden="true" class="glyphicon glyphicon-cog"></span>权限设置</button>
			</div>
		    </div>					            
		<div class="tab-content">
			<div class="tab-pane active" id="tab_1">
				<table id="deptTable" class="table table-bordered table-hover" >
					<thead><tr>
							<th>部门名称</th><th>部门标识</th><th>部门经理</th>
							<th>全标识</th><th>所属机构</th><th>操作</th>
							</tr>
					</thead>
					<tbody id="deptList"></tbody>
				</table>
			</div>
					              <!-- /.tab-pane -->
		</div>
					            <!-- /.tab-content -->
	  </div>
					          <!-- nav-tabs-custom -->
	</form>
</div>
<script type="text/template" id="deptListTemplate">
   <? for ( var i = 0; i < rows.length; i++) {?>
		<tr id="deptRow_<?=i+1?>">
		<th style="display:none;"><?=i+1?></th>
		<td><input type="text" class="form-control" value="<?=rows[i].deptCname?>" id="deptCname_<?=i+1?>" name="deptCname" / ></td>
		<td><input type="text" class="form-control" value="<?=rows[i].deptEname?>" id="deptEname_<?=i+1?>" name="deptEname" / ></td>
		<td><input type="text" value="<?=rows[i].deptRemark?>" class="form-control" id="deptRemark_<?=i+1?>" name="deptRemark" / ></td>
		<td><input type="text" class="form-control" value="<?=rows[i].fullEname?>" id="fullEname_<?=i+1?>"  name="fullEname" readonly/ ></td>
		<td class="col-xs-2">
			<input type="hidden" class="form-control" value="<?=rows[i].deptId?>" id="deptId_<?=i+1?>" name="deptId"  / >
			<input type="text" class="form-control" id="orgEname_<?=i+1?>" name="orgEname" value="<?=rows[i].orgEname?>" readonly / >
		</td>
		<td>
            <button type="button" class="btn btn-success" onclick="saveDept(<?=i+1?>);">保存</button>
        </td>
	</tr>
  <?}?>
</script>
<script type="text/template" id="addRowDept">
	<tr id="deptRow_<?=index?>">
		<td>
			<input type="text" class="form-control" value="" id="deptCname_<?=index?>"  name="deptCname" / >
		</td>
		<td>
			<input type="text" class="form-control" value="" id="deptEname_<?=index?>"  name="deptEname"/ >
		</td>
		<td>
			<input type="text" class="form-control" value="" id="fullEname_<?=index?>"  name="fullEname"/ >
		</td>
		<td class="col-xs-2">
			<input type="hidden" class="form-control" value="<?=orgId?>" id="orgId_<?=index?>" name="orgId" / >
			<input type="text" class="form-control" id="orgEname_<?=index?>" name="orgEname" value="<?=orgEname?>" readonly / >
		</td>
		<td>
			<input type="text" value="" class="form-control" name="deptRemark" id="deptRemark_<?=index?>" / >
		</td>
		<td>
			<button type="button" class="btn btn-success" onclick="saveDept(<?=index?>);">保存</button>
		</td>
	</tr>				
</script>
<script>
function getDeptList(orgId,fatherId){
	deleteAllRow("deptTable");
	var paramsData={};
	paramsData.doCode="deptList";
	paramsData.orgId=orgId;
	if(fatherId!=null){
		paramsData.fatherId=fatherId;
	}
	$.ajax({
			type:'get',
			url:common_list_url,
		    dataType:'json', 
		    data:paramsData, 
		    async: false,
		    success: function(myData){
			   //console.log(myData);
				index=myData.total;
				var data = baidu.template("deptListTemplate",myData); 
				$("#deptList").append(data);
		   }
		});	
  }
  
function addRowDept(){
	var myData = {};
	myData.orgEname = ename;
	myData.orgId = orgId;
	myData.index = index+1;
	index = index+1;
	var data = baidu.template("addRowDept",myData); 
	$("#deptList").append(data);
}

function saveDept(num){
	if(num==0){ num = deptNum;}//如果num为0，则说明是没有选中数据便点击了修改。
	if(num=="undefined" || num =="" || num == null){
		alert("请先选中一条数据！");
		return;
	}else{
		var paramsData={};
		paramsData.deptCname = $("#deptCname_"+num).val();
		paramsData.deptEname = $("#deptEname_"+num).val();
		paramsData.fullEname = $("#fullEname_"+num).val();
		paramsData.orgId = $("#orgId_"+num).val();
		paramsData.orgEname = $("#orgEname_"+num).val();
		paramsData.deptRemark = $("#deptRemark_"+num).val();
		paramsData.doCode="deptSave";
		paramsData.method="save";
		var deptId =$("#deptId_"+num).val();
		if(deptId !=null && deptId!=""){
			paramsData.deptId = deptId;
		}
		$.ajax({type:'post',url:common_submit_url,
		    data:paramsData,dataType:'json',
		    success: function(myData){
				console.log('save.dept',myData);
		    	if(myData.flag=='T'){
					layer.alert("操作成功");
					getDeptList(orgId,fatherId);
		    	}else{

				}
		  	}
		});
	}
}

function delDept(num){
	if(num==0){num = deptNum;}//如果num为0，则说明是没有选中数据便点击了修改。
	if(num=="undefined" || num =="" || num == null){
		alert("请先选中一条数据！");
		return;
	}else{
		var paramsData={};
		paramsData.doCode="deptDel";
		paramsData.method="delete";
		var deptId =$("#deptId_"+num).val();
		if(deptId !=null && deptId!=""){
			paramsData.deptId = deptId;
		}
		$.ajax({type:'post', url:common_submit_url,
		    data:paramsData, dataType:'json',
		    success: function(myData){
		   // console.log(myData);
		      	layer.alert('操作成功！', {  closeBtn: 0 });
		    	if(myData.flag=='T'){
		    	   getDeptList(orgId,fatherId);
		    	}
		  	}
		});
	}
}
function deptUserList(num){
    if(num==0){num = deptNum;}//如果num为0，则说明是没有选中数据便点击了修改。
	if(num=="undefined" || num =="" || num == null){
		alert("请先选中一条数据！");
		return;
	}else{
	   var deptId =$("#deptId_"+num).val();
	   $("#deptUsersDiv").load("${ctx}/template/uct/dept-user-list.html",function(){getDeptUserListData(deptId);
	       $('#dept_users_modal').modal('show');
		$('#role-actsTable tbody').on('click','tr', function (e) {
		     if ( $(this).hasClass('selected') ) {
		        }  else {
		            $('tr.selected').removeClass('selected');
		            $(this).addClass('selected');
		        }
		     deptNum = $('tr.selected th').eq(0).html();
		   });
	   });
	}
}
function deptActsList(num){
    if(num==0){num = deptNum;}//如果num为0，则说明是没有选中数据便点击了修改。
	if(num=="undefined" || num =="" || num == null){
		alert("请先选中一条数据！");
		return;
	}else{
	   var deptId =$("#deptId_"+num).val();
	   $("#deptActsDiv").load("${ctx}/template/uct/dept-acts.html",function(){getDeptActListData(deptId,"0");
	       $('#dept_acts_modal').modal('show');
		$('#role-actsTable tbody').on('click','tr', function (e) {
		     if ( $(this).hasClass('selected') ) {
		        }  else {
		            $('tr.selected').removeClass('selected');
		            $(this).addClass('selected');
		        }
		     deptNum = $('tr.selected th').eq(0).html();
		   });
	   });
	}
  }
</script>